package cn.xlz.wechat.web.util;

import java.util.Random;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSONObject;

public class StringUtil {

	/** 
	* @Title: randomClickNumber 
	* @Description: 随机数字 0 - 40 
	* @param @return   
	* @return int   
	* @throws 
	*/
	public static int randomClickNumber(){
		int random = new Random().nextInt(40);
		if(random == 0){
			random++;
		}
		return random;
	}
	
	/** 
	* @Title: getRandomString 
	* @Description: 随机字符串
	* @param @param length
	* @param @return   
	* @return String   
	* @throws 
	*/
	public static String getRandomString(int length) { //length表示生成字符串的长度  
	    String base = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";     
	    Random random = new Random();     
	    StringBuffer sb = new StringBuffer();     
	    for (int i = 0; i < length; i++) {     
	        int number = random.nextInt(base.length());     
	        sb.append(base.charAt(number));     
	    }     
	    return sb.toString();     
	 } 
	
	/** 
	* @Title: getJson 
	* @Description: 组装返回格式
	* @param @param code
	* @param @param msg
	* @param @param date
	* @param @return   
	* @return JSONObject   
	* @throws 
	*/
	public static String getJson(int code,String msg,String data){
		JSONObject json = new JSONObject();
		json.put("code", code);
		json.put("msg", msg);
		if(StringUtils.isNotEmpty(data)){
			try {
				json.put("data", data);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return json.toJSONString();
	}
	public static void main(String[] args) {
		System.out.println(getRandomString(16));
	}
	
	/*
	 * 判断字符串是否包含一些字符 contains
	 */
	public static boolean containsString(String src, String dest) {
		boolean flag = false;
		if (src.contains(dest)) {
			flag = true;
		}
		return flag;
	}
	
	/*
	 * 判断字符串是否包含一些字符 indexOf
	 */
	public static boolean indexOfString(String src, String dest) {
		boolean flag = false;
		if (src.indexOf(dest)!=-1) {
			flag = true;
		}
		return flag;
	}
	
}
